﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统字典类型服务
    /// </summary>
    public class DicCategoryService : BaseService<SysDicCategory>
    {
        public static readonly DicCategoryService Instance = new DicCategoryService();
        protected override string RootUrl => "/api/sys/dic/";

        /// <summary>
        /// 新建字典类型模型
        /// </summary>
        public SysDicCategory New()
        {
            return new SysDicCategory
            {
                Id = NewId()
            };
        }

        /// <summary>
        /// 复制字典类型模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysDicCategory Copy(SysDicCategory original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }

        /// <summary>
        /// 检查字典类型编码是否存在
        /// </summary>
        /// <param name="code">字典类型编码</param>
        public bool ExistByCode(string code)
        {
            var url = GetUrl("existDicCategoryByCode", code);
            return SysRuntime.Api.PostAsMessage(url).EnsureSuccess().Success;
        }

        /// <summary>
        /// 新建字典类型
        /// </summary>
        /// <param name="model">字典类型模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysDicCategory model)
        {
            var url = GetUrl("insertDicCategory");
            return SysRuntime.Api.PostJsonAsMessage(url, model).EnsureSuccess().Success;
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="model">字典类型模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysDicCategory model)
        {
            var url = GetUrl("updateDicCategory");
            return SysRuntime.Api.PostJsonAsMessage(url, model).EnsureSuccess().Success;
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            var url = GetUrl("deleteDicCategory");
            var map = Map.New.Add(SysConst.FIdsName, ids);
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 根据主键获取字典类型对象
        /// </summary>
        /// <param name="id">字典类型主键</param>
        public SysDicCategory SelectById(string id)
        {
            var url = GetUrl("selectDicCategoryById", id);
            return SysRuntime.Api.Post<SysDicCategory>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 根据字典类型编码获取字典类型对象
        /// </summary>
        /// <param name="code">字典类型编码</param>
        /// <returns>成功返回模型对象</returns>
        public SysDicCategory SelectByCode(string code)
        {
            var url = GetUrl("selectDicCategoryByCode", code);
            return SysRuntime.Api.Post<SysDicCategory>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询字典类型列表
        /// </summary>
        public List<SysDicCategory> SelectList()
        {
            var url = GetUrl("selectListDicCategory");
            return SysRuntime.Api.Post<List<SysDicCategory>>(url).EnsureSuccess().Data;
        }
    }
}
